<?php

require_once('admincontroller.php');

class Medias extends AdminController
{
	function Medias()
	{
		parent::AdminController();
		$this->load->helper('file');
		$this->main_navigation = 'medias';
	}

	function index()
	{
		$this->files();
	}

	// files -------------------------------------------------------------------

	function files()
	{
		$data['files'] = get_filenames(ADMIN_UPLOAD_DIR, true);
		$this->sub_navigation = 'files';
		$this->display('file_list', $data);
	}

	function file_new()
	{
		$this->sub_navigation = 'files';
		$this->display('file_new');
	}

	function file_upload()
	{
		$this->sub_navigation = 'files';
		$config['upload_path'] = ADMIN_UPLOAD_DIR;
		$config['max_size']	= '0';
		$config['allowed_types'] = '*';
		$this->load->library('upload', $config);
		if (!$this->upload->do_upload()) {
			$data['error_notice'] = $this->upload->display_errors();
			$this->display('file_new', $data);
		}
		else {
			$res = $this->upload->data();
			$data['success_notice'] = 'Le fichier <strong>'.$res['file_name'].'</strong> a été envoyé';
			$data['files'] = get_filenames(ADMIN_UPLOAD_DIR, true);
			$this->display('file_list', $data);
		}
	}

	function file_delete()
	{
		if (isset($_POST['filename'])) {
			$path = ADMIN_UPLOAD_DIR.'/'.clean_str($_POST['filename']);

			if (unlink($path)) {
				return;
			}
		}
		$this->output->set_status_header(400);
	}

	function download()
	{
		$filename = substr(strrchr($_SERVER['QUERY_STRING'], '/'), 1);
		$path = ADMIN_UPLOAD_DIR.'/'.$filename;
		if (file_exists($path)) {
			header('Content-Disposition: attachment; filename="'.$filename.'"');
			header('Content-Type: '.mime_content_type($path));
			header('Content-Transfer-Encoding: binary');
			header('Content-Length: '. filesize($path));
			header('Pragma: no-cache');
			header('Accept-Ranges: bytes');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Expires: 0');
			readfile($path);
			die();
		}
		$data['files'] = get_filenames(ADMIN_UPLOAD_DIR, true);
		$data['error_notice'] = 'Impossible de télécharger le fichier <b>'.$filename.'</b>';
		$this->sub_navigation = 'files';
		$this->display('file_list', $data);
	}

	function picture_galleries()
	{
		$this->sub_navigation = 'pictures';
		$this->display('picture_galleries');
	}
}
